package de.preamp.clients.java.lib.ui;

import java.awt.Toolkit;
import java.awt.datatransfer.StringSelection;
import java.awt.event.ActionEvent;
import java.io.IOException;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.JPopupMenu;

import org.json.JSONException;

import de.preamp.clients.java.data.AbstractDBItem;
import de.preamp.clients.java.data.Album;
import de.preamp.clients.java.data.Artist;
import de.preamp.clients.java.data.Track;
import de.preamp.clients.java.net.DownloadManager;

/**
 * @author André Stannek
 * @version 2009-09-03
 */
public class ItemContextMenueUI extends JPopupMenu
{

    /**
     * 
     */
    private static final long serialVersionUID = 7034674153506797316L;

    /**
     * @param selectedValue
     *            the currently selected item
     */
    public ItemContextMenueUI(final AbstractDBItem selectedValue)
    {
	if (selectedValue instanceof Track)
	{
	    this.add(new CopyAction((Track) selectedValue));
	    this.addSeparator();
	}
	this.add(new DownloadDefaultAction(selectedValue));
	this.add(new DownloadAction(selectedValue));
    }

    /**
     * Copies the URL to a track to the clipboard.
     * 
     * @author André Stannek
     * @since 2010-03-28
     * @version 2010-03-28
     */
    private static class CopyAction extends AbstractAction
    {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7489422307275078037L;

	/**
	 * 
	 */
	private final Track item;

	/**
	 * @param item
	 *            the item the menu is opened on
	 */
	public CopyAction(final Track item)
	{
	    super("copy http-link");
	    this.item = item;
	}

	@Override
	public void actionPerformed(final ActionEvent e)
	{
	    final StringSelection url =
		    new StringSelection(item.getURL().toString());
	    Toolkit.getDefaultToolkit().getSystemClipboard().setContents(url,
		    null);

	}
    }

    /**
     * This action lets the user choose a download folder and download the
     * selected item to it.
     * 
     * @author André Stannek
     * @see preamp.client.java.net.DowloadManager
     */
    private static class DownloadAction extends AbstractAction
    {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8571215979632536673L;

	/**
	 * 
	 */
	private final AbstractDBItem item;

	/**
	 * @param item
	 *            the item the menu is opened on
	 */
	public DownloadAction(final AbstractDBItem item)
	{
	    super("download to...");
	    this.item = item;
	}

	@Override
	public void actionPerformed(final ActionEvent event)
	{
	    final Thread thread = new Thread()
	    {
		@Override
		public void run()
		{
		    super.run();
		    final JFileChooser fc = new JFileChooser();
		    fc.setDialogTitle("select folder to dowload "
			    + item.toString() + " to...");
		    fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
		    fc.setAcceptAllFileFilterUsed(false);
		    if (fc.showSaveDialog(null) == JFileChooser.APPROVE_OPTION)
		    {
			if (item instanceof Track)
			{

			    try
			    {
				JOptionPane
					.showMessageDialog(null, "downloading"
						+ item.toString() + " to "
						+ fc.getSelectedFile(),
						"downloading...",
						JOptionPane.INFORMATION_MESSAGE);
				DownloadManager.download((Track) item, fc
					.getSelectedFile());
			    }
			    catch (final IOException e)
			    {
				JOptionPane
					.showMessageDialog(
						null,
						"Local file could not be created. "
							+ "Make sure you have access rights "
							+ "and enough free disk space.",
						"download error",
						JOptionPane.ERROR_MESSAGE);
			    }
			}
			if (item instanceof Album)
			{
			    try
			    {
				JOptionPane
					.showMessageDialog(null, "downloading"
						+ item.toString() + " to "
						+ fc.getSelectedFile(),
						"downloading...",
						JOptionPane.INFORMATION_MESSAGE);
				DownloadManager.download((Album) item, fc
					.getSelectedFile());
			    }
			    catch (final IOException e)
			    {
				JOptionPane
					.showMessageDialog(
						null,
						"Local file could not be created. "
							+ "Make sure you have access rights "
							+ "and enough free disk space.",
						"download error",
						JOptionPane.ERROR_MESSAGE);
			    }
			}
			if (item instanceof Artist)
			{
			    try
			    {
				JOptionPane
					.showMessageDialog(null, "downloading"
						+ item.toString() + " to "
						+ fc.getSelectedFile(),
						"downloading...",
						JOptionPane.INFORMATION_MESSAGE);
				DownloadManager.download((Artist) item, fc
					.getSelectedFile());
			    }
			    catch (final IOException e)
			    {
				JOptionPane
					.showMessageDialog(
						null,
						"Local file could not be created. "
							+ "Make sure you have access rights "
							+ "and enough free disk space.",
						"download error",
						JOptionPane.ERROR_MESSAGE);
			    }
			    catch (final JSONException e)
			    {
				JOptionPane
					.showMessageDialog(
						null,
						"Got invalid result from server. "
							+ "Try running an update on the server.",
						"server connection error",
						JOptionPane.ERROR_MESSAGE);
			    }
			}
		    }
		}
	    };
	    thread.start();
	}

	@Override
	public boolean isEnabled()
	{
	    return true;
	}
    }

    /**
     * Download the selected item to default download folder.
     * 
     * @author André Stannek
     * @see preamp.client.java.net.DowloadManager
     */
    private static class DownloadDefaultAction extends AbstractAction
    {

	/**
	 * 
	 */
	private static final long serialVersionUID = -8571215979632536673L;

	/**
	 * 
	 */
	private final AbstractDBItem item;

	/**
	 * @param item
	 *            the item the menu is opened on
	 */
	public DownloadDefaultAction(final AbstractDBItem item)
	{
	    super("download");
	    this.item = item;
	}

	@Override
	public void actionPerformed(final ActionEvent event)
	{
	    final Thread thread = new Thread()
	    {

		@Override
		public void run()
		{
		    super.run();
		    if (item instanceof Track)
		    {
			try
			{
			    JOptionPane.showMessageDialog(null, "downloading"
				    + item.toString()
				    + " to default download folder...",
				    "downloading...",
				    JOptionPane.INFORMATION_MESSAGE);
			    DownloadManager.download((Track) item);
			}
			catch (final IOException e)
			{
			    JOptionPane.showMessageDialog(null, "downloading"
				    + item.toString()
				    + " to default download folder...",
				    "downloading...",
				    JOptionPane.INFORMATION_MESSAGE);
			}
		    }
		    else if (item instanceof Album)
		    {
			try
			{
			    JOptionPane.showMessageDialog(null, "downloading"
				    + item.toString()
				    + " to default download folder...",
				    "downloading...",
				    JOptionPane.INFORMATION_MESSAGE);
			    DownloadManager.download((Album) item);
			}
			catch (final IOException e)
			{
			    JOptionPane
				    .showMessageDialog(
					    null,
					    "Local file could not be created. "
						    + "Make sure you have access rights "
						    + "and enough free disk space.",
					    "download error",
					    JOptionPane.ERROR_MESSAGE);
			}
		    }
		    else if (item instanceof Artist)
		    {
			try
			{
			    JOptionPane.showMessageDialog(null, "downloading"
				    + item.toString()
				    + " to default download folder...",
				    "downloading...",
				    JOptionPane.INFORMATION_MESSAGE);
			    DownloadManager.download((Artist) item);
			}
			catch (final IOException e)
			{
			    JOptionPane
				    .showMessageDialog(
					    null,
					    "Local file could not be created. "
						    + "Make sure you have access rights "
						    + "and enough free disk space.",
					    "download error",
					    JOptionPane.ERROR_MESSAGE);
			}
			catch (final JSONException e)
			{
			    JOptionPane
				    .showMessageDialog(
					    null,
					    "Got invalid result from server. "
						    + "Try running an update on the server.",
					    "server connection error",
					    JOptionPane.ERROR_MESSAGE);
			}
		    }
		}
	    };
	    thread.start();
	}

	@Override
	public boolean isEnabled()
	{
	    return true;
	}
    }
}
